<template>
	<view>
		<view v-if="!userInfo.idCardFront">
			<view class="card-imgs">
				<uploadFile title="身份证正面" :previewFullImage="false" @setFile="setFile($event, 'id_card_front')"/>
				<uploadFile title="身份证反面" :previewFullImage="false" @setFile="setFile($event, 'id_card_back')"/>
			</view>
			<view class="card-imgs cleas">
				<uploadFile title="营业执照" :previewFullImage="false" @setFile="setFile($event, 'business_license')"/>
			</view>
			<view class="btn-boxfs">
				<view hover-class="hover_class" @click="submitsd">提交</view>
			</view>
		</view>
		<view v-else>
			<view class="card-imgs">
				<image mode="aspectFill" :src="userInfo.idCardFront" @click="priview(0)"></image>
				<image mode="aspectFill" :src="userInfo.idCardBack" @click="priview(1)"></image>
			</view>
			<view class="card-imgs cleas" v-if="userInfo.businessLicense">
				<image mode="aspectFill" :src="userInfo.businessLicense" @click="priview(2)"></image>
			</view>
		</view>
	</view>
</template>

<script>
import uploadFile from '@/component/uploadFile/index.vue'
import {
	idCardSave,
	userIndex
} from '@/utils/api/user.js'
import {baseUrl} from '@/utils/api/request.js'

export default {
	components: {
		uploadFile
	},
	data() {
		return {
			formInfo: {
				id_card_front: '',
				id_card_back: '',
				business_license: ''
			},
			userInfo: {}
		}
	},
	mounted() {
		this.getInfo()
	},
	methods: {
		priview(index) {
			uni.previewImage({
				current: index,
				urls: [this.userInfo.idCardFront, this.userInfo.idCardBack, this.userInfo.businessLicense]
			  });
		},
		getInfo() {
			uni.showLoading({
				title: '加载中'
			})
			userIndex().then((res) => {
				this.userInfo = res.data
			}).finally(() => {
				uni.hideLoading()
			})
		},
		setFile(data, key) {
			if(data && data.length !== 0) {
				this.formInfo[key] = data[0].url
			} else {
				this.formInfo[key] = ''
			}
		},
		submitsd() {
			const {id_card_front, id_card_back} = this.formInfo
			if(!id_card_front || !id_card_back) {
				return uni.showToast({
					title: '请上传身份信息',
					icon: 'none'
				})
			}
			uni.showLoading({
				title: '提交中'
			})
			idCardSave(this.formInfo).then((res) => {
				uni.hideLoading()
				uni.showToast({
					title: '提交成功',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 800)
			})
		}
	}
}
</script>

<style scoped lang="scss">
.btn-boxfs {
	box-sizing: border-box;
	padding: 0 30upx;
	margin-top: 70upx;
	>view {
		width: 100%;
		height: 90upx;
		border-radius: 50upx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #023F92;
		font-size: 32upx;
		color: #fff;
	}
}
.card-imgs {
	display: flex;
	align-items: center;
	justify-content: space-between;
	box-sizing: border-box;
	padding: 40upx 30upx;
	>image {
		width: 330upx !important;
		height: 230upx !important;
	}
	::v-deep .upload-box {
		width: 330upx !important;
		height: 230upx !important;
		>view {
			font-size: 27upx !important;
			margin-top: 24upx;
		}
	}
	::v-deep .u-upload__wrap__preview {
		width: 330upx !important;
		height: 230upx !important;
		image {
			width: 330upx !important;
			height: 230upx !important;
		}
	}
}
.cleas {
	padding-top: 0 !important;
}
</style>